package com.holly.unit.gen.modular.controller;

import com.holly.unit.core.pojo.response.ErrorResponseData;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.gen.api.config.GenCodeCfg;
import com.holly.unit.gen.api.pojo.TableInfo;
import com.holly.unit.gen.modular.pojo.TableInfoRequest;
import com.holly.unit.gen.modular.service.GenCodeService;
import com.holly.unit.gen.modular.service.TableDataService;
import com.holly.unit.genx.util.DatabaseExtUtil;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述: 代码生成器业务模块
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/1/15 15:16
 */
@RestController
@ApiResource(name = "代码生成器相关接口")
@Api(tags = "代码生成器相关接口")
public class GenCodeController {

    @Resource
    private GenCodeService genCodeService;

    @Resource
    private TableDataService tableDataService;

    /**
     * 代码生成
     *
     * @author holly
     * @date 2020/12/27 13:17
     */
    @ApiOperation(value = "代码生成" ,notes = "代码生成")
    @PostResource(name = "代码生成", path = "/gencode/generator", requiredPermission = false,requiredLogin = false)
    public ResponseData generator(@RequestBody TableInfoRequest tableInfoRequest) {
        List<String> res = new ArrayList<>();
        try {
            TableInfo table = new TableInfo();
            BeanUtils.copyProperties(tableInfoRequest,table);
            res = this.genCodeService.generate(table,"");
        } catch (Exception e) {
            return new ErrorResponseData("holly-d-gen","代码生成报错，请查看配置项");
        }
        return new SuccessResponseData(res);
    }

    /**
     * 代码生成
     *
     * @author holly
     * @date 2020/12/27 13:17
     */
    @ApiOperation(value = "查询所有的表信息" ,notes = "查询所有的表信息")
    @GetResource(name = "查询所有的表信息", path = "/gencode/tables", requiredPermission = false,requiredLogin = false)
    public ResponseData tables(@RequestParam(value = "tableName",required = false) String tableName,@RequestParam(value = "tableComment",required = false) String tableComment) {

        Map<String,Object> param = new HashMap<>();
        param.put("tableSchema", GenCodeCfg.dbName);
        param.put("tableName",tableName);
        param.put("tableComment",tableComment);
        return new SuccessResponseData(tableDataService.fetchTableDataPage(param));
    }

    /**
     * 判断表在数据库是否存在
     * @author holly
     * @date 2020/12/27 13:17
     */
    @ApiOperation(value = "判断表在数据库是否存在" ,notes = "判断表在数据库是否存在")
    @GetResource(name = "判断表在数据库是否存在", path = "/gencode/checkTable", requiredPermission = false,requiredLogin = false)
    public ResponseData checkTable(@RequestParam(value = "tableName") String tableName) {
        return new SuccessResponseData(DatabaseExtUtil.checkTableExist(tableName));
    }

}
